package com.phuoc.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.phuoc.dao.TypeOfEstablishmentDAO;
import com.phuoc.dao.TypeOfFoodDAO;

public class WelcomeActivity extends Activity {

	private TypeOfEstablishmentDAO typeEstabDataSource;
	private TypeOfFoodDAO typeFoodDataSource;

	// Dialog
	private ProgressDialog dialogWaiting;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);

		// Open database connection
		typeEstabDataSource = new TypeOfEstablishmentDAO(this);
		typeEstabDataSource.open();
		typeFoodDataSource = new TypeOfFoodDAO(this);
		typeFoodDataSource.open();

		// create data type of estab, type of food
		createData();

	}

	@Override
	protected void onPause() {
		typeEstabDataSource.close();
		typeFoodDataSource.close();
		super.onPause();
	}

	@Override
	protected void onResume() {
		typeEstabDataSource.open();
		typeFoodDataSource.open();
		super.onResume();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnStart:
			dialogWaiting = ProgressDialog.show(this, "Loading...",
					"Loading all establishment");
			Thread th = new Thread(new Runnable() {
				public void run() {
					startActivity(new Intent(WelcomeActivity.this,
							ListEstablishmentActivity.class));
					dialogWaiting.dismiss();
				}
			});
			th.start();
			break;

		default:
			break;
		}
	}

	// waiting 3s
	// Thread splashThread = new Thread() {
	// @Override
	// public void run() {
	// try {
	// int waited = 0;
	// while (waited < 3000) {
	// sleep(100);
	// waited += 100;
	// }
	// } catch (InterruptedException e) {
	// // do nothing
	// } finally {
	// startActivity(new Intent(WelcomeActivity.this,
	// EstablishmentActivity.class));
	// finish();
	//
	// }
	// }
	// };
	// splashThread.start();

	// create data type of estab, type of food
	private void createData() {
		// type of estab
		if (!typeEstabDataSource.checkNameExists("Bar")) {
			typeEstabDataSource.createTypeOfEstablishment("Bar");
		}
		if (!typeEstabDataSource.checkNameExists("Restaurant")) {
			typeEstabDataSource.createTypeOfEstablishment("Restaurant");
		}
		if (!typeEstabDataSource.checkNameExists("Cafe")) {
			typeEstabDataSource.createTypeOfEstablishment("Cafe");
		}

		// type of food
		if (!typeFoodDataSource.checkNameExists("Fish and Chips")) {
			typeFoodDataSource.createTypeOfFood("Fish and Chips");
		}
		if (!typeFoodDataSource.checkNameExists("Pizza")) {
			typeFoodDataSource.createTypeOfFood("Pizza");
		}
		if (!typeFoodDataSource.checkNameExists("Tapas")) {
			typeFoodDataSource.createTypeOfFood("Tapas");
		}
	}
}
